<template lang="html">
  <div class="account-box">
    <div class="toggle">
      <!-- 是否短信登录 -->
      <a @click="isMsgLogin = false" href="#" v-if="isMsgLogin">
        <i class="iconfont icon-user"></i> 使用账号登录
      </a>
      <a @click="isMsgLogin = true" href="#" v-else>
        <i class="iconfont icon-msg"></i> 使用短信登录
      </a>
    </div>
    <!-- 表单验证, 禁止浏览器自动填充表单 -->
    <VeeForm class="form" :validationSchema="schema" v-slot="{errors}" ref="formCom" autocomplete="off">
      <!-- 账号密码登录 -->
      <template v-if="!isMsgLogin">
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-user"></i>
            <VeeField :class="{error:errors.account}" v-model="form.account" name="account" type="text"
                      placeholder="请输入用户名"></VeeField>
          </div>
          <div class="error" v-if="errors.account"><i class="iconfont icon-warning"/>{{ errors.account }}</div>
        </div>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-lock"></i>
            <VeeField :class="{error:errors.password}" v-model="form.password" type="password" name="password"
                      placeholder="请输入密码"/>
          </div>
          <div class="error" v-if="errors.password"><i class="iconfont icon-warning"/>{{ errors.password }}</div>
        </div>
      </template>
      <!-- 手机验证码登录 -->
      <template v-else>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-user"></i>
            <VeeField :class="{error:errors.phoneNumber}" name="phoneNumber" v-model="form.phoneNumber" type="text"
                      placeholder="请输入手机号"/>
          </div>
          <div class="error" v-if="errors.phoneNumber"><i class="iconfont icon-warning"/>请输入正确手机号</div>
        </div>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-code"></i>
            <VeeField :class="{error:errors.code}" name="code" v-model="form.code" type="text"
                      placeholder="请输入验证码"/>
            <span class="code">发送验证码</span>
          </div>
          <div class="error" v-if="errors.code"><i class="iconfont icon-warning"/>请输入正确验证码</div>
        </div>
      </template>
      <a @click="login()" href="#" class="btn">登录</a>
    </VeeForm>
    <div class="action">
      <img src="https://qzonestyle.gtimg.cn/qzone/vas/opensns/res/img/Connect_logo_7.png" alt="">
      <div class="url">
        <a href="#">忘记密码</a>
        <a href="#">免费注册</a>
      </div>
    </div>
  </div>
</template>
<script>
import {ref, reactive, watch} from 'vue'
import schema from '@/utils/vee-validate-schema'
import {Form as VeeForm, Field as VeeField} from 'vee-validate';
import {userAccountLogin} from '@/api/user';
import message from '@/components/library/message';
// import {useUserStore} from '@/store/modules/user';
import {useRoute, useRouter} from 'vue-router';
import {useIntervalFn} from '@vueuse/core';

export default {
  name: 'LoginForm',
  components: {
    VeeField,
    // Vee提供的表单组件
    VeeForm,
  },
  setup() {
    const isMsgLogin = ref(false)

    // 通过ref获取组件VeeForm
    const formCom = ref(null)

    watch(isMsgLogin, () => {
      form.account = null
      form.password = null
      form.phoneNumber = null
      form.code = null
      // 重置表单
      formCom.value.resetForm()
    })

    // 表单信息容器
    const form = reactive({
      account: 'wyb',
      password: '123456',
      phoneNumber: null,
      code: null
    })


    // 校验规则对象
    const mySchema = {
      account: schema.account,
      password: schema.password,
      phoneNumber: schema.phoneNumber,
      code: schema.code,
    }

    // 表单信息提交
    const login = async () => {
      const result = await userAccountLogin(form.account, form.password)
      if(result) {
        console.log("this is result",result)

        // 存储用户信息
        // 提示
        message({type: 'success', text: '登录成功'})
        // 跳转
        // route.query.redirectUrl ||
        await router.push('/')
      }else {
        message({type:'failed', text:'登陆失败'})
      }

    }

    // const userStore = useUserStore()

    // 获取路由实例
    const router = useRouter()

    // 获取路由信息
    //eslint-disable-next-line
    const route = useRoute()

    // 验证码发送时间间隔
    const time = ref(0)
    // useIntervalFn(回调，执行间隔，立刻开启)
    const {pause, resume} = useIntervalFn(() => {
      time.value--
      if (time.value <= 0) {
        pause()
      }
    }, 1000, false)


    // const send = async () => {
    //   const valid = mySchema.phoneNumber(form.phoneNumber)
    //   if (valid) {
    //     if (time.value === 0) {
    //       await userMobileLoginMsg(form.phoneNumber)
    //       message({type: 'success', text: '发送成功'})
    //       time.value = 60
    //       resume()
    //     }
    //   } else {
    //     formCom.value.setFieldError('phoneNumber', valid)
    //   }
    // }


    return {
      isMsgLogin,
      form,
      schema: mySchema,
      formCom,
      login,
      time
    }
  }
}
</script>
<style lang="less" scoped>
// 账号容器
.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;

    a {
      color: @xtxColor;

      i {
        font-size: 14px;
      }
    }
  }

  .form {
    padding: 0 40px;

    &-item {
      margin-bottom: 28px;

      .input {
        position: relative;
        height: 36px;

        > i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }

        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;

          &.error {
            border-color: @priceColor;
          }

          &.active, &:focus {
            border-color: @xtxColor;
          }
        }

        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }

      > .error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: @priceColor;

        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }

    .agree {
      a {
        color: #069;
      }
    }

    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: @xtxColor;

      &.disabled {
        background: #cfcdcd;
      }
    }
  }

  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}
</style>